package com.cysd.ewmxns.sdw_ui.activity

import android.os.Bundle
import android.util.Log
import com.bytedance.sdk.openadsdk.CSJSplashAd
import com.cszy.ewmxns.base.BaseVMActivity
import com.cysd.ewmxns.sdw_utils.SpUtils
import com.cysd.ewmxns.sdw_viewmodel.MainViewModel
import com.hlyj.http.base.tool.lib_hlyj_base.Constants
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseAppConstans
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseModuleSpUtils
import com.hlyj.http.base.tool.lib_hlyj_base.utils.JsonUtil
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener
import com.hnyyac.ad.mds.utils.BaseAdConstants
import com.hnyyac.ad.mds.views_notload.IAdviewNotLoadSplashListener
import com.hnyyac.ad.mds.views_notload.NewGromoreSplashAd
import com.ok.common.api.GlobalApi
import qrrspeeelild.rqrc0ode.andro0ild.databinding.ActivitySplashBinding

import java.util.concurrent.atomic.AtomicBoolean

class SingleHotSplashActivity : BaseVMActivity<MainViewModel, ActivitySplashBinding>(),IAdViewListener {
    private lateinit var splashAd: NewGromoreSplashAd
    private val isGoMainCalled = AtomicBoolean(false)
    override fun initView(savedInstanceState: Bundle?) {
        splashAd = NewGromoreSplashAd(this, object : IAdviewNotLoadSplashListener {
            override fun onTrigger(adUnitId: String) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.TRIGGER,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                //  Log.e("ProductEnvConstance", "onTrigger${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdShow(ad: CSJSplashAd, adUnitId: String, position :Int) {
                //  Log.e("splash", "onAdShow${ad.mediationManager.showEcpm.sdkName}")

                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.SHOW,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to position,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        Constants.DATA to JsonUtil.parseStringToJsonWithMsg(ad.mediationManager.showEcpm.sdkName)//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClick(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.CLICK,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to ocCodee,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClose(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.CLOSE,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to ocCodee,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
                goToMainActivity()


            }

            override fun onError(msg:String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.FAIL,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        // Constants.PLACEMENTID to splash,
                        Constants.OCCODE to ocCodee,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        //  Constants.DATA to JsonUtil.parseStringToJsonWithMsg(code),
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                GlobalApi.sendLog(logContent)

                if (isGoMainCalled.compareAndSet(false, true)) {

                    goToMainActivity()
                }

            }

            override fun onSuccess(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.LOAD,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ,1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to ocCodee,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onSkipped() {

            }

            override fun goMain() {


            }
        })
    }


    override fun initData() {
        mBinding.lottieView.start()

        //重置所有tab 狀態為可點擊
        SpUtils.getInstance().putBoolean(BaseAppConstans.IS_show_TAB1,false)
        SpUtils.getInstance().putBoolean(BaseAppConstans.IS_show_TAB2,false)
        SpUtils.getInstance().putBoolean(BaseAppConstans.IS_show_TAB3,false)
        loadSplash()
    }
    private fun loadSplash() {
        Log.d("qin", "loadSplash")
        splashAd?.load(
            adUnitId = BaseAdConstants.SPLASH_ID_1,
            container = mBinding.contentLayout,
            time = 5000L,
        )
    }
    /**
     *进入主页
     */
    private fun goToMainActivity() {
        AdManager.loadChapin_nocache(
            this@SingleHotSplashActivity,
            21,
            this@SingleHotSplashActivity,
            object : AdLoadListener {
                override fun onAdLoadFailed(ad: IAdView?) {

                }
            })
        finish()
    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {

    }

    override fun onError(msg: String) {

    }

    override fun onLoadError(adView: IAdView) {

    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {

    }
    override fun onDestroy() {
        super.onDestroy()
        splashAd.onDestroy()
        mBinding.lottieView.stop()
    }
}